<?php
include "./common.php";
define("WALL_LEFT_START",0);
define("WALL_LEFT_END",760);//最大960-200
define("WALL_TOP_START",0);
define("WALL_TOP_END",478);//656-152

if(empty($_GET['job']) || ($_GET['job']=='view' && $_GET['type']!='small')){
	$page = $_GET['page'];
	$pageSize = $_GET['pageSize'];
	!$page && $page = 1;
	!$pageSize && $pageSize = 50;


	$limit = "LIMIT ".($page-1)*$pageSize.",$pageSize";

	$query = $db->query("SELECT * FROM ".tname("pray")." ORDER BY sendtime DESC $limit");
	while($array = $db->fetch_array($query)){
		$array['left'] = rand(WALL_LEFT_START,WALL_LEFT_END)."px";
		$array['top'] = rand(WALL_TOP_START,WALL_TOP_END)."px";
		$prays[] = $array;
	}
	$amount = $db->get_one("SELECT COUNT(ID) as amount FROM ".tname("pray"));

	$pages = multi($amount['amount'],$pageSize,$page,"lovewall.php");


	include template("lovewall");
}else if($_GET['job']=='view'){
	$pray = $db->get_one("SELECT * FROM ".tname('pray')." WHERE id='$_GET[id]'");
	if($pray==null){
		showclose("编号$_GET[id] 祝福不存在!");
	}

	extract($pray);
	if($_GET['type']=='small'){
		
		include template("viewlovesmall");

	}else{
		include template("viewlove");
	}
}else if($_GET['job']=='add'){
	if(empty($_POST['submit'])){
		include template("writelove");
	}else{
		if($_POST['senduser']==""){
			$$_POST['senduser'] = "匿名";
		}

		$timestamp = time();
		$db->query("INSERT INTO ".tname('pray')." SET title='$_POST[title]',receiveuser='$_POST[receiveuser]',senduser='$_POST[senduser]',receiveemail='$_POST[receiveemail]',content='$_POST[content]',sendtime='$timestamp'");
		$id = $db->insert_id();
		/**
		 * 发送邮件
		 **/
		file_get_contents("http://denghui.xacitywall.com/lovewall.php?job=sendmail&id=$id");
		
		showmessage("祝福发送成功","lovewall.php?job=view&id=$id");
	}
}else if($_GET['job']=='sendmail'){
	require_once("sendmail.php");
	
	$id = $_GET['id'];
	$content = $db->get_one("SELECT * FROM ".tname('pray')." WHERE id='$id'");
	
	if(!empty($content['receiveemail'])){
		require_once("dataconfig.php");
		$content['loveurl'] = "http://denghui.xacitywall.com/lovewall.php?job=view&id=".$content['id'];
		$content['sendtime'] = date("Y-m-d H:i:s",$content['sendtime']);
		$_CONFIG['mailtitle'] =replaceVar($_CONFIG['mailtitle'],$content);
		$_CONFIG['mailcontent'] =replaceVar($_CONFIG['mailcontent'],$content);

		$mail = new smtp($_CONFIG['server'],25,true,$_CONFIG['email'],$_CONFIG['password']);
		$mail->sendmail($content['receiveemail'],$_CONFIG['email'],$_CONFIG['mailtitle'],$_CONFIG['mailcontent'],"HTML");
		
	}
}
$content = array('id'=>'100','title'=>'测试','content'=>'内容测试');

function replaceVar($content,$arr){
	$content = str_replace('\\','',$content);
	foreach($arr as $key=>$val){
		$content = str_replace('{$'.$key.'}',$val,$content);
	}
	return $content;
}

?>